import { Navigate, useRoutes } from "react-router-dom";
import Login from "@/views/login/index";

// * 导入所有router
// const files = require.context('.', false, /\.ts$/);
const metaRouters = import.meta.globEager("./modules/*.tsx");

// * 处理路由
export const routerArray: any = [];

Object.keys(metaRouters).forEach(item => {
	Object.keys(metaRouters[item]).forEach((key: string) => {
		return routerArray.push(...metaRouters[item][key]);
	});
});

export const rootRouter = [
	{
		path: "/",
		element: <Navigate to="/login" />
	},
	{
		path: "/login",
		element: <Login />,
		meta: {
			requiresAuth: false,
			title: "登录页",
			key: "login"
		}
	},
	...routerArray,
	{
		path: "*",
		element: <Navigate to="/404" />
	}
];

const Router = () => {
	const routes = useRoutes(rootRouter);
	return routes;
};

export default Router;

/*
require 自动引入实现 
	const files = require.context('.', false, /\.ts$/); // 参1表示检索的目录 参2表示是否检索子文件夹 参3匹配文件的正则表达式,一般是文件名
	const modules = {};
	files.keys().forEach((key) => {
			if (key === './index.ts') { return; }
			modules[key.replace(/(\.\/|\.ts)/g, '')] = files(key).default;
	});
	export default modules;

import 自动引入实现
	const files = import.meta.globEager("./*.ts")
	const modules: any = {};

	for (const key in files) {
			if (Object.prototype.hasOwnProperty.call(files, key)) {
					modules[key.replace(/(\.\/|\.ts)/g, '')] = files[key].default
			}
	}
	export default modules;

*/
